package org.koyad.portal.filters;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.koyad.portal.constants.FrameworkConstants;
import org.koyad.portal.context.PistonContext;
import org.koyad.portal.parser.PageParser;
import org.koyad.portal.parser.TagsHandler;
import org.koyad.portal.tags.AbstractTag;
import org.koyad.portal.tags.ViewRoot;
import org.koyad.portal.utils.LogUtils;
import org.koyad.portal.vo.PageMetaData;

public class PageUtils {

	private static final LogUtils LOGGER = LogUtils.getLogger(PageUtils.class);
	
	public static void include(String path, HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		RequestDispatcher reqDisp = req.getRequestDispatcher(path);
		reqDisp.include(req, resp);
	}
	
	public static void setLogin(HttpServletRequest req, HttpServletResponse resp) {

		StringBuffer buffer = new StringBuffer(req.getRequestURI());
		if (null != req.getQueryString()) {
			buffer.append("?" + req.getQueryString());
		}

		String redirectURL = buffer.toString();
		if (redirectURL.equals(req.getContextPath() + "/" + FrameworkConstants.PISTON_FWK)) {
			redirectURL = req.getContextPath() + "/pages/home";
		}

		Cookie redirectCookie = new Cookie(FrameworkConstants.PISTON_REDIRECT_URL, redirectURL);
		redirectCookie.setPath(req.getContextPath() + "/");
		redirectCookie.setHttpOnly(true);
		resp.addCookie(redirectCookie);

		PageMetaData page = new PageMetaData(null, "/xml/login.xml");
		createViewRoot(page);
		
		//This is to create session before login
		req.getSession(PistonContext.getContext().getViewRoot().isSession());
	}
	
	public static void createViewRoot(PageMetaData metaData) {
		ViewRoot viewRoot = new ViewRoot(metaData);
		//doing it here is necessary as this ViewRoot is referenced by form tag in parsing phase 
		PistonContext.getContext().setViewRoot(viewRoot);
		
		AbstractTag theme = null;
		AbstractTag page = null;
		
		if(null != metaData.getPath()) {
			page = getTree(metaData.getPath());
			if(null != page.getAttributeValue("frame")) {
				theme = getTree("/frames/" + page.getAttributeValue("frame") + "/Frame.xml");
			}
		} else {
			String pagePath = "";
			if(metaData.getPortalPage().isInternal()) {
				pagePath = "D:/Tools/piston-pages/internal/" +  metaData.getPortalPage().getPageId() + ".xml";
			} else {
				pagePath = "D:/Tools/piston-pages/external/" +  metaData.getPortalPage().getPageId() + ".xml";
			}
			page = PageUtils.getTreeFS(pagePath);
			theme = getTree("/frames/" + metaData.getPortalPage().getFrame() + "/Frame.xml");
		}
		
		viewRoot.setPage(page);
		viewRoot.setTheme(theme);
	}
	
	/*
	 * create portal page view
	 */
	public static AbstractTag getTreeFS(String path) {
		InputStream is = null;
		try {
			is = new FileInputStream(new File(path));
		} catch (FileNotFoundException e) {
			LOGGER.logException(e);
		}
		return PageParser.parseTree(is, new TagsHandler());
	}
	
	/*
	 * create theme/action view
	 */
	public static AbstractTag getTree(String path) {
		PistonContext pistonCtx = PistonContext.getContext();
		InputStream is = pistonCtx.getRequest().getServletContext().getResourceAsStream(path);
		return PageParser.parseTree(is, new TagsHandler());
	}
	
	
	
	

}
